const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const Pet = sequelize.define('Pet', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false
  },
  species: {
    type: DataTypes.STRING,
    allowNull: false
  },
  level: {
    type: DataTypes.INTEGER,
    defaultValue: 1
  },
  experience: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  energy: {
    type: DataTypes.INTEGER,
    defaultValue: 100
  },
  happiness: {
    type: DataTypes.INTEGER,
    defaultValue: 100
  },
  hunger: {
    type: DataTypes.INTEGER,
    defaultValue: 100
  },
  mood: {
    type: DataTypes.INTEGER,
    defaultValue: 100
  },
  appearance: {
    type: DataTypes.JSON,
    allowNull: false
  },
  isNFT: {
    type: DataTypes.BOOLEAN,
    defaultValue: false
  },
  tokenId: {
    type: DataTypes.STRING,
    allowNull: true
  },
  lastFed: {
    type: DataTypes.DATE,
    allowNull: true
  },
  lastTrained: {
    type: DataTypes.DATE,
    allowNull: true
  },
  introduction: {
    type: DataTypes.STRING,
    allowNull: true
  },
  userId: {
    type: DataTypes.INTEGER,
    allowNull: true  // 允许为空，表示还未被领养的宠物
  }
}, {
  timestamps: true,
  createdAt: 'createdAt',
  updatedAt: 'updatedAt'
});

// 定义关联关系
Pet.associate = (models) => {
  Pet.belongsTo(models.User, {
    foreignKey: 'userId',
    as: 'owner',
    onDelete: 'SET NULL',
    onUpdate: 'CASCADE'
  });
};

module.exports = Pet; 